//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System.Collections;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;
using CommunityServer.Galleries.Components;

namespace CommunityServer.Galleries.Controls
{

	public class GalleryDetails : GalleryThemedControl
	{

		private bool hideWhenInCategory = false;
		/// <summary>
		/// Causes the control not to render if there is a category defined in the context
		/// </summary>
		public bool HideWhenInCategory
		{
			get { return hideWhenInCategory; }
			set { hideWhenInCategory = value; }
		}

		#region Child Controls

		private IText name;
		private IText description;
		private IText totalCategories;
		private IText totalPictures;
		private IText UpdateDateLabel;
		private HyperLink FirstAlbum; 
		private HyperLink LastAlbum;
		private Panel PagerPanel;

		#endregion

		#region Skin

		protected override void AttachChildControls()
		{
			name = FindText( "Name" );
			description = FindText( "Description" );
			totalCategories = FindText( "TotalCategories" );
			totalPictures = FindText( "TotalPictures" );
			UpdateDateLabel = FindText( "UpdateDate" );
			FirstAlbum = (HyperLink)FindControl( "FirstAlbum" );
			LastAlbum = (HyperLink)FindControl( "LastAlbum" );
			PagerPanel = (Panel)FindControl ( "PagerPanel" );
		}

		#endregion

		#region Event Overrides

		protected override void OnLoad(System.EventArgs e)
		{
			base.OnLoad(e);
			this.DataBind();
		}

		public override void DataBind()
		{
			base.DataBind();
			BindData();

		}

		#endregion

		private void BindData()
		{
			if(HideWhenInCategory && CSContext.Current.CategoryID > 0)
			{
				this.Visible = false;
				return;
			}

			if(name != null)
				name.Text = CurrentGallery.Name;

			if(description != null)
			{
				description.Text = CurrentGallery.Description.Replace("\n", "<br/>");
				if(description.Text.Trim() != string.Empty && !description.Text.EndsWith("<br/>"))
					description.Text += "<br/>";
			}

			ArrayList categories = PostCategories.GetCategories(CurrentGallery.SectionID, false, false, -1);
			PostCategory.SortBy = CurrentGallery.CategorySortBy;
			categories.Sort() ;

			if(totalCategories != null)
				totalCategories.Text = categories.Count.ToString();

			if(PagerPanel != null)
				PagerPanel.Visible = CurrentGallery.CategorizationType == CategorizationType.Album && (categories.Count > 1);

			if(FirstAlbum != null )
			{
				if(categories.Count > 0)
					FirstAlbum.NavigateUrl = GalleryUrls.Instance().ViewCategory(CurrentGallery.ApplicationKey, ((PostCategory)categories[0]).CategoryID);
				else
					FirstAlbum.Visible = false;
			}

			if(LastAlbum != null )
			{
				if(categories.Count > 0)
					LastAlbum.NavigateUrl = GalleryUrls.Instance().ViewCategory(CurrentGallery.ApplicationKey, ((PostCategory)categories[categories.Count - 1]).CategoryID); 
				else
					LastAlbum.Visible = false;
			}

			if(totalPictures != null)
				totalPictures.Text = CurrentGallery.TotalThreads.ToString();
			
			if(UpdateDateLabel != null)
				UpdateDateLabel.Text = CurrentGallery.MostRecentPostDate.ToShortDateString();
		}



	}
}
